package com.itcast.googleplay.fragment;

import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.widget.ListView;
import com.itcast.googleplay.R;
import com.itcast.googleplay.adapter.MyBaseAdapter;
import com.itcast.googleplay.bean.CategoryBean;
import com.itcast.googleplay.holder.BaseHolder;
import com.itcast.googleplay.holder.GridHolder;
import com.itcast.googleplay.holder.TitleHolder;
import com.itcast.googleplay.protocol.CategoryProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.UIUtils;

public class CategoryFragment extends BaseFragment {
	private List<CategoryBean> listCategory;
	private List<Object> allList = null;

	@Override
	public ResultState onSubLoad() {
		CategoryProtocol categoryProtocol = new CategoryProtocol();
		listCategory = categoryProtocol.getData("category", 0, "");
		if(listCategory!=null){
			if(listCategory.size()>0){
				return ResultState.STATE_SUCCESSED;
			}else{
				return ResultState.STATE_EMTPY;
			}
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSuccessedSubView() {
		View view = UIUtils.inflate(R.layout.layout_list_view_category);
		ListView lvCategory = (ListView) view.findViewById(R.id.lv_item);
		allList = new ArrayList<Object>();
		for (CategoryBean categoryBean : listCategory) {
			allList.add(categoryBean.title);
			allList.addAll(categoryBean.infos);
		}
		
		MyAdapter myAdapter = new MyAdapter(allList);
		lvCategory.setAdapter(myAdapter);
		
		return view;
	}
	
	class MyAdapter extends MyBaseAdapter<Object>{
		public static final int ITEM_TITLE = 0;
		public static final int ITEM_GRID = 1;
		
		@Override
		public int getViewTypeCount() {
			return super.getViewTypeCount()+1;
		}
		@Override
		public int getItemViewType(int position) {
			Object object = getItem(position);
			if(object instanceof String){
				return ITEM_TITLE;
			}else{
				return ITEM_GRID;
			}
		}
		public MyAdapter(List<Object> list) {
			super(list);
		}
		@Override
		public BaseHolder<Object> getHolder() {
			int itemTyep = getItemViewType(currentPosition);
			if(itemTyep == ITEM_TITLE){
				return new TitleHolder();
			}else{
				return new GridHolder();
			}
		}
	}
}
